<?php

class Work {
    private $wid;
    private $uid;
    private $pid;
    private $ptid;
    private $timeStart;
    private $timeEnd;
    private $ended; 
    
    public function getWid() {
        return $this->wid;
    }

    public function getUid() {
        return $this->uid;
    }

    public function getPid() {
        return $this->pid;
    }

    public function getPtid() {
        return $this->ptid;
    }

    public function getTimeStart() {
        return $this->timeStart;
    }

    public function getTimeEnd() {
        return $this->timeEnd;
    }

    public function getEnded() {
        return $this->ended;
    }

    private function setData($data) {
        foreach($data as $key => $val) {   
            if(property_exists(get_class($this),$key)) {
                $this->$key = $val;
            }
        }
    }
    
    public function start($user, $task) {
        if($user->getUid() < 1) {
            $err = Error::instance();
            $err->add("Niepoprawny użytkownik.");
            return false;
        }
        
        if($task->getPtid() < 1) {
            $err = Error::instance();
            $err->add("Niepoprawne zadanie.");
            return false;
        }
        
        if(WorkDAO::hasTasksAtProgress($user)) {
            $err = Error::instance();
            $err->add("Użytkownik posiada już rozpoczęte zadanie");
            return false;
        }
        
        if($task->atProgress()) {
            $err = Error::instance();
            $err->add("Zadanie jest aktualnie w trakcie wykonywania.");
            return false;
        }
        
        $this->ptid = $task->getPtid();
        $this->uid = $user->getUid();
        $this->pid = $task->getPid();
        $this->wid = WorkDAO::startWork($this);
        return $this->wid;
    }
    
    public function getCurrentWork($user) {
        if($user->getUid() < 1) {
            return false;
        }
        
        $work = WorkDAO::getCurrentWork($user);
        if(count($work)) {
            $this->setData($work);
        }
        return $this->wid;
    }
    
    public function stop($user) {
        if(!$this->getCurrentWork($user)) {
            return false;
        }
        $this->timeEnd = date("Y-m-d H:i:s");
        $this->ended = 1;
        WorkDAO::endWork($this);
        return strtotime($this->timeEnd) - strtotime($this->timeStart);
    }
    
    public function getWorkDone($startDate,$endDate, $project=false, $user=false) {
        if(!is_int($startDate)) {
            $startDate = strtotime($startDate);
        }
        if(!is_int($endDate)) {
            $endDate = strtotime($endDate);
        }
        if($startDate > $endDate) {
            $tmp = $startDate;
            $startDate = $endDate;
            $endDate = $tmp;
        }
        return WorkDAO::getWorkDone($startDate, $endDate, $project, $user);
    }
    
    public function getHours($startDate, $endDate, $user, $which=0) { //0 all 1 normal(pn-pt) 2 weekends 3 trips
        if($user->getUid() < 1) {
            return false;
        }
        if(!is_int($startDate)) {
            $startDate = strtotime($startDate);
        }
        if(!is_int($endDate)) {
            $endDate = strtotime($endDate);
        }
        if($startDate > $endDate) {
            $tmp = $startDate;
            $startDate = $endDate;
            $endDate = $tmp;
        }
        return WorkDAO::getHours($startDate, $endDate, $user, $which);
    }
    
    public function getHoursByProjects($startDate,$endDate,$user) {
        if($user->getUid() < 1) {
            return false;
        }
        if(!is_int($startDate)) {
            $startDate = strtotime($startDate);
        }
        if(!is_int($endDate)) {
            $endDate = strtotime($endDate);
        }
        if($startDate > $endDate) {
            $tmp = $startDate;
            $startDate = $endDate;
            $endDate = $tmp;
        }
        return WorkDAO::getHoursByProjects($startDate, $endDate, $user);
    }
    
    public function getHoursByTasks($startDate,$endDate,$user) {
        if($user->getUid() < 1) {
            return false;
        }
        if(!is_int($startDate)) {
            $startDate = strtotime($startDate);
        }
        if(!is_int($endDate)) {
            $endDate = strtotime($endDate);
        }
        if($startDate > $endDate) {
            $tmp = $startDate;
            $startDate = $endDate;
            $endDate = $tmp;
        }
        return WorkDAO::getHoursByTasks($startDate, $endDate, $user);
    }
    
    public function getTaskWork($task) {
        if($task->getPtid() < 1) {
            return false;
        }
        return WorkDAO::getTaskWork($task);
    }
    
    public function getWorkTimeString($workStd, $secondsGiven=false) {
        if($secondsGiven) {
            $seconds = $workStd;
        } else {
            $seconds = strtotime($workStd->time_end) - strtotime($workStd->time_start);
        }
        $hours = (int)($seconds / 3600);
        $seconds = $seconds % 3600;
        $minutes = (int)($seconds / 60);
        $seconds = $seconds % 60;
        $timeString = '';
        if($hours > 0) {
            $timeString .= $hours . ':';
        }
        
        if($minutes > 0) {
            if($minutes < 10) {
                $timeString .= '0' . $minutes .':';
            } else {
                $timeString .= $minutes .':';
            }
        } else if($hours > 0) {
            $timeString .= '00:';
        }
        
        if($seconds > 0) {
            if($seconds < 10 && ($minutes > 0 || $hours > 0)) {
                $timeString .= '0' . $seconds;
            } else {
                $timeString .= $seconds;
            }
        } else if($minutes > 0 || $hours > 0) {
            $timeString .= '00';
        }
        return $timeString;
    }
}

?>
